{% extends "base.html" %}
{% load static %}
{% load account socialaccount %}

{% block title %}注册{% endblock title %}

{% block content %}
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8 col-lg-6">
            <!-- 消息提示 -->
            {% if messages %}
            <div class="messages mt-4">
                {% for message in messages %}
                <div class="alert alert-{{ message.tags }} alert-dismissible fade show" role="alert">
                    {{ message }}
                    <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                </div>
                {% endfor %}
            </div>
            {% endif %}

            <div class="card shadow-sm mt-4">
                <div class="card-body p-5">
                    <h2 class="text-center mb-4">创建账号</h2>

                    <!-- 社交账号注册 -->
                    {% get_providers as socialaccount_providers %}
                    {% if socialaccount_providers %}
                    <div class="social-login mb-4">
                        <p class="text-center text-muted">使用社交账号快速注册</p>
                        <div class="d-flex justify-content-center gap-3 mb-4">
                            {% include "socialaccount/snippets/provider_list.html" with process="login" %}
                        </div>
                        <div class="divider">
                            <span class="divider-text">或者填写注册信息</span>
                        </div>
                    </div>
                    {% endif %}

                    <!-- 注册表单 -->
                    <form method="post" action="." class="needs-validation" novalidate>
                        {% csrf_token %}
                        
                        <!-- 昵称 -->
                        <div class="form-group mb-3">
                            <label for="username" class="form-label">昵称</label>
                            <div class="input-group">
                                <span class="input-group-text">
                                    <i class="fas fa-user"></i>
                                </span>
                                <input type="text" class="form-control" id="username" name="username" 
                                       placeholder="请输入昵称" required>
                            </div>
                            <div class="invalid-feedback">请输入昵称</div>
                        </div>

                        <!-- 邮箱 -->
                        <div class="form-group mb-3">
                            <label for="email" class="form-label">邮箱</label>
                            <div class="input-group">
                                <span class="input-group-text">
                                    <i class="fas fa-envelope"></i>
                                </span>
                                <input type="email" class="form-control" id="email" name="email" 
                                       placeholder="your@email.com" required>
                            </div>
                            <div class="invalid-feedback">请输入有效的邮箱地址</div>
                        </div>

                        <!-- 密码 -->
                        <div class="form-group mb-3">
                            <label for="password" class="form-label">密码</label>
                            <div class="input-group">
                                <span class="input-group-text">
                                    <i class="fas fa-lock"></i>
                                </span>
                                <input type="password" class="form-control" id="password" name="password" 
                                       placeholder="请设置密码" required>
                            </div>
                            <div class="invalid-feedback">请设置密码</div>
                        </div>

                        <!-- 确认密码 -->
                        <div class="form-group mb-4">
                            <label for="password2" class="form-label">确认密码</label>
                            <div class="input-group">
                                <span class="input-group-text">
                                    <i class="fas fa-lock"></i>
                                </span>
                                <input type="password" class="form-control" id="password2" name="password2" 
                                       placeholder="请确认密码" required>
                            </div>
                            <div class="invalid-feedback">两次输入的密码不一致</div>
                        </div>

                        <!-- 提交按钮 -->
                        <button type="submit" class="btn btn-primary w-100 mb-3">
                            创建账号
                        </button>

                        <p class="text-center mb-0">
                            已有账号？
                            <a href="{% url 'account_login' %}" class="text-primary">
                                立即登录
                            </a>
                        </p>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

<style>
/* 消息提示样式 */
.alert {
    border-radius: 8px;
    border: none;
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

.alert-success {
    background-color: #d4edda;
    color: #155724;
}

.alert-error,
.alert-danger {
    background-color: #f8d7da;
    color: #721c24;
}

.btn-close {
    font-size: 0.8rem;
}

/* 卡片样式 */
.card {
    border: none;
    border-radius: 15px;
}

/* 分隔线样式 */
.divider {
    position: relative;
    text-align: center;
    margin: 20px 0;
}

.divider::before {
    content: '';
    position: absolute;
    left: 0;
    top: 50%;
    width: 100%;
    height: 1px;
    background: #e0e0e0;
}

.divider-text {
    background: #fff;
    padding: 0 15px;
    color: #6c757d;
    position: relative;
    font-size: 14px;
}

/* 表单样式 */
.form-control {
    border-radius: 8px;
    padding: 10px 15px;
}

.input-group-text {
    background-color: transparent;
    border-right: none;
}

.form-control:focus {
    border-color: #80bdff;
    box-shadow: 0 0 0 0.2rem rgba(0,123,255,.25);
}

/* 社交登录按钮样式 */
.socialaccount_providers {
    list-style: none;
    padding: 0;
    margin: 0;
    display: flex;
    gap: 15px;
    justify-content: center;
}

.socialaccount_provider {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 8px 20px;
    border-radius: 8px;
    text-decoration: none;
    transition: all 0.3s ease;
    font-weight: 500;
}

/* 按钮样式 */
.btn-primary {
    padding: 10px;
    border-radius: 8px;
    font-weight: 500;
}

/* 响应式调整 */
@media (max-width: 576px) {
    .card-body {
        padding: 2rem !important;
    }
}
</style>

<script>
// 表单验证
(function () {
    'use strict'
    var forms = document.querySelectorAll('.needs-validation')
    Array.prototype.slice.call(forms).forEach(function (form) {
        form.addEventListener('submit', function (event) {
            if (!form.checkValidity()) {
                event.preventDefault()
                event.stopPropagation()
            }
            
            // 检查密码是否匹配
            var password = document.getElementById('password')
            var password2 = document.getElementById('password2')
            if (password.value !== password2.value) {
                password2.setCustomValidity('密码不匹配')
                event.preventDefault()
                event.stopPropagation()
            } else {
                password2.setCustomValidity('')
            }
            
            form.classList.add('was-validated')
        }, false)
    })
})()
</script>
{% endblock content %}